﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Text.RegularExpressions;

namespace Gift.Utile
{
    public class CustomList : DataList
    {
        // Fields
        private const string AMP = "&amp;";
        private const string COMMA = "?";
        private int currentPageIndex;
        private IList dataSource;
        protected string emptyText;
        private string HTML1 = "<div class=page><span>";
        protected const string HTML2 = "</span>";
        protected const string HTML3 = "<p>";
        protected const string HTML4 = "</p></div>";
        private int itemCount;
        private const string KEY_PAGE = "page";
        private const string LINK_DISPLAY = "<a href=?page={0}>&nbsp;{1}&nbsp;</a>";
        private const string LINK_MORE = "| <a href=?page={0}>下一页 \x00bb &nbsp;</a>";
        private const string LINK_PREV = "<a href=?page={0}>&nbsp;\x00ab 上一页</a> |";
        private int pageSize = 12;
        private static readonly Regex RX = new Regex("&amp;page=d+", RegexOptions.Compiled);

        // Events
        public event DataGridPageChangedEventHandler PageIndexChanged;

        // Methods
        protected override void OnDataBinding(EventArgs e)
        {
            int num = this.CurrentPageIndex * this.pageSize;
            int num2 = Math.Min(this.pageSize, this.ItemCount - num);
            IList list = new ArrayList();
            for (int i = 0; i < num2; i++)
            {
                list.Add(this.dataSource[num + i]);
            }
            base.DataSource = list;
            base.OnDataBinding(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            if (this.Visible)
            {
                string s = this.Context.Request["page"];
                int index = (s != null) ? (int.Parse(s) - 1) : 0;
                this.SetPage(index);
            }
        }

        protected virtual void OnPageIndexChanged(DataGridPageChangedEventArgs e)
        {
            if (this.PageIndexChanged != null)
            {
                this.PageIndexChanged(this, e);
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (this.ItemCount == 0)
            {
                writer.Write(this.emptyText);
            }
            else
            {
                string input = this.Context.Request.Url.Query.Replace("?", "&amp;").Replace("&amp;page=" + Convert.ToInt32((int)(this.currentPageIndex + 1)), string.Empty).Replace("&amp;page=" + this.currentPageIndex, string.Empty).Replace("&amp;page=", string.Empty);
                input = RX.Replace(input, string.Empty);
                base.Render(writer);
                writer.Write(this.HTML1);
                writer.Write(string.Concat(new object[] { "&nbsp; 显示：共&nbsp;", Convert.ToInt64((int)(this.PageCount + 1)), "&nbsp;页&nbsp;", this.itemCount, "&nbsp 条" }));
                writer.Write("</span>");
                writer.Write("<p>");
                if (this.currentPageIndex > 0)
                {
                    writer.Write(string.Format("<a href=?page={0}>&nbsp;\x00ab 上一页</a> |", this.currentPageIndex + input));
                }
                else
                {
                    writer.Write("&nbsp;\x00ab 上一页 |");
                }
                if (((this.PageCount + 1) > 10) && ((this.currentPageIndex + 1) >= 10))
                {
                    writer.Write(string.Format("<a href=?page={0}>&nbsp;{1}&nbsp;</a>", 1 + input, 1));
                    writer.Write("...");
                }
                for (int i = 0; ((i <= 9) && (i <= this.PageCount)) && (((this.currentPageIndex - (this.currentPageIndex % 10)) + i) <= this.PageCount); i++)
                {
                    if (((this.currentPageIndex - (this.currentPageIndex % 10)) + i) == this.currentPageIndex)
                    {
                        writer.Write("&nbsp;<b>" + (this.currentPageIndex + 1) + "</b>&nbsp;");
                    }
                    else
                    {
                        writer.Write(string.Format("<a href=?page={0}>&nbsp;{1}&nbsp;</a>", (((this.currentPageIndex - (this.currentPageIndex % 10)) + i) + 1) + input, ((this.currentPageIndex - (this.currentPageIndex % 10)) + i) + 1));
                    }
                }
                if (((this.PageCount + 1) > 10) && ((this.currentPageIndex + 1) <= 10))
                {
                    writer.Write("...");
                    writer.Write(string.Format("<a href=?page={0}>&nbsp;{1}&nbsp;</a>", (this.PageCount + 1) + input, this.PageCount + 1));
                }
                if (this.currentPageIndex < this.PageCount)
                {
                    writer.Write(string.Format("| <a href=?page={0}>下一页 \x00bb &nbsp;</a>", (this.currentPageIndex + 2) + input));
                }
                else
                {
                    writer.Write("|&nbsp;下一页 \x00bb&nbsp;");
                }
                writer.Write("</p></div>");
            }
        }

        public void SetPage(int index)
        {
            this.OnPageIndexChanged(new DataGridPageChangedEventArgs(null, index));
        }

        // Properties
        public virtual int CurrentPageIndex
        {
            get
            {
                return this.currentPageIndex;
            }
            set
            {
                this.currentPageIndex = value;
            }
        }

        public override object DataSource
        {
            set
            {
                try
                {
                    this.dataSource = (IList)value;
                    this.ItemCount = this.dataSource.Count;
                }
                catch
                {
                    this.dataSource = null;
                    this.ItemCount = 0;
                }
            }
        }

        public string EmptyText
        {
            set
            {
                this.emptyText = value;
            }
        }

        protected virtual int ItemCount
        {
            get
            {
                return this.itemCount;
            }
            set
            {
                this.itemCount = value;
            }
        }

        protected int PageCount
        {
            get
            {
                return ((this.ItemCount - 1) / this.pageSize);
            }
        }

        public int PageSize
        {
            get
            {
                return this.pageSize;
            }
            set
            {
                this.pageSize = value;
            }
        }
    }


}
